#include "widget.h"
#include "ui_widget.h"

#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    if (ui) {
        delete ui;
        ui = nullptr;
    }
}

void Widget::showBoolInfo(QString str, bool checkValue)
{
    if (checkValue) {
        ui->plainTextEdit->appendPlainText(str + " = true");
    } else {
        ui->plainTextEdit->appendPlainText(str + " = false");
    }
}

void Widget::on_pushButtonFrontBack_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===front(), back()函数测试");
    QString str1 = ui->comboBoxStr1->currentText();
    ui->plainTextEdit->appendPlainText("str1 = " + str1);

    ui->plainTextEdit->appendPlainText("front() = " + QString(str1.front()));
    ui->plainTextEdit->appendPlainText("back() = " + QString(str1.back()));
}

void Widget::on_pushButtonLeftRight_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===left(), right()函数测试");

    QString str1 = ui->comboBoxStr1->currentText();
    ui->plainTextEdit->appendPlainText("str1 = " + str1);

    QString str2 = str1.left(2);
    ui->plainTextEdit->appendPlainText("left(2) = " + str2);

    str2 = str1.right(6);
    ui->plainTextEdit->appendPlainText("right(6) = " + str2);
}

void Widget::on_pushButtonFirstLast_clicked()
{ // 使用 mid 函数 替代 first 和 last
    ui->plainTextEdit->appendPlainText("\n===first(), last()函数测试");

    QString str1 = ui->comboBoxStr1->currentText();  //获取str1输入
    QString str2 = ui->comboBoxStr2->currentText();  //获取str2输入
    ui->plainTextEdit->appendPlainText("str1 = " + str1);
    ui->plainTextEdit->appendPlainText("str2 = " + str2);

    int num = str1.lastIndexOf(str2);    //str2出现的最后位置

    QString str3 = str1.mid(0, num + 1);    //路径名称,带最后的"\"
    ui->plainTextEdit->appendPlainText(str3);

    str3= str1.mid(num + 1, -1);   //去除路径的文件名
    ui->plainTextEdit->appendPlainText(str3);
}

void Widget::on_pushButtonSection_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===section()函数测试");

    QString str1 = ui->comboBoxStr1->currentText();
    int num = ui->spinBox->value();
    QString str2 = ui->comboBoxStr2->currentText(); //用于分隔的字符

    QString str3;
    if ( QString::compare(str2, "\\", Qt::CaseInsensitive) == 0 ) {
        str3 = str1.section('\\', num, num); //字符‘\’需要单独处理
    } else {
        str3 = str1.section(str2, num, num); //其他分隔字符，如‘,’
    }

    ui->plainTextEdit->appendPlainText(str3);
}

void Widget::on_pushButtonIsNull_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===isNull()函数测试");

    QString str1, str2 = "";
    ui->plainTextEdit->appendPlainText("QString str1, str2 = \"\"");
    showBoolInfo("\nstr1.isNull()", str1.isNull());
    showBoolInfo("str1.isEmpty()", str1.isEmpty());

    showBoolInfo("\nstr2.isNull()", str2.isNull());
    showBoolInfo("str2.isEmpty()", str2.isEmpty());

    str2.clear();
    ui->plainTextEdit->appendPlainText("\nstr2.clear() 后");
    showBoolInfo("\nstr2.isNull()",str2.isNull());
    showBoolInfo("str2.isEmpty()", str2.isEmpty());
}

void Widget::on_pushButtonResize_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===resize()函数测试");

    QString str1;
    str1.resize(5, '0');       //str1="00000"
    ui->plainTextEdit->appendPlainText(str1);

    str1.resize(10, QChar(0x54C8));   //str1="00000哈哈哈哈哈", 0x54C8是‘哈’的Unicode编码
    ui->plainTextEdit->appendPlainText(str1);
}

void Widget::on_pushButtonSizeLen_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===size(),count(),length()函数测试");
    QString str1 = ui->comboBoxStr1->currentText();
    ui->plainTextEdit->appendPlainText(str1);
    int num = str1.size();
    ui->plainTextEdit->appendPlainText(QString::asprintf("size()=%d", num));
    num = str1.count();
    ui->plainTextEdit->appendPlainText(QString::asprintf("count()=%d", num));
    num = str1.length();
    ui->plainTextEdit->appendPlainText(QString::asprintf("length()=%d", num));
}

void Widget::on_pushButtonFill_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===fill()函数测试");

    QString str1="Hello";
    str1.fill('X');     //str1=”XXXXX”
    ui->plainTextEdit->appendPlainText(str1);

    str1.fill('A',2);   //str1=”AA”，改变了长度
    ui->plainTextEdit->appendPlainText(str1);

    str1.fill(QChar(0x54C8),3); //str1=”哈哈哈”, 0x54C8是‘哈’的UTF-16编码
    ui->plainTextEdit->appendPlainText(str1);
}

void Widget::on_pushButtonIndexOf_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===indexOf(),lastIndexOf()函数测试");

    QString str1 = ui->comboBoxStr1->currentText();
    QString str2 = ui->comboBoxStr2->currentText();
    ui->plainTextEdit->appendPlainText("str1 = " + str1);
    ui->plainTextEdit->appendPlainText("str2 = " + str2);

    int num = str1.indexOf(str2);
    QString info=QString::asprintf("indexOf() = %d", num);
    ui->plainTextEdit->appendPlainText(info);

    num = str1.lastIndexOf(str2);
    info = QString::asprintf("lastIndexOf() = %d", num);
    ui->plainTextEdit->appendPlainText(info);
}

void Widget::on_pushButtonContains_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===contains()函数测试");
    QString str1 = ui->comboBoxStr1->currentText();
    QString str2 = ui->comboBoxStr2->currentText();
    ui->plainTextEdit->appendPlainText(str1);
    ui->plainTextEdit->appendPlainText(str2);

    bool chk = str1.contains(str2);
    showBoolInfo("contains()", chk);
}

void Widget::on_pushButtonEndsWith_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===endsWith(),startsWith()函数测试");

    QString str1 = ui->comboBoxStr1->currentText();
    QString str2 = ui->comboBoxStr1->currentText();
    ui->plainTextEdit->appendPlainText("str1 = " + str1);
    ui->plainTextEdit->appendPlainText("str2 = " + str2);

    bool chk = str1.endsWith(str2);
    showBoolInfo("endsWith()", chk);
    chk = str1.startsWith(str2);
    showBoolInfo("startsWith()", chk);
}

void Widget::on_pushButtonCount_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===count()统计子字符串出现的次数");

    QString str1 = ui->comboBoxStr1->currentText();
    QString str2 = ui->comboBoxStr2->currentText();
    ui->plainTextEdit->appendPlainText(str1);
    ui->plainTextEdit->appendPlainText(str2);

    int num = str1.count(str2,Qt::CaseSensitive);
    ui->plainTextEdit->appendPlainText(QString::asprintf("count()= %d", num));
}

void Widget::on_pushButtonTrimmed_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===trimmed(),simplified()函数测试");
    QString str1 = ui->comboBoxStr1->currentText();

    ui->plainTextEdit->appendPlainText("str1 = " + str1);

    QString str2 = str1.trimmed();
    ui->plainTextEdit->appendPlainText("trimmed() = " + str2);

    str2 = str1.simplified();
    ui->plainTextEdit->appendPlainText("simplified() = " + str2);
}

void Widget::on_pushButtonInsert_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===insert()函数测试");
    QString str1="It is great";
    ui->plainTextEdit->appendPlainText(str1);

    int num = str1.lastIndexOf(" ");
    str1.insert(num, "n't");
    ui->plainTextEdit->appendPlainText(str1);
}

void Widget::on_pushButtonRemove_clicked()
{
    //==== remove()移除特定的字符
    ui->plainTextEdit->appendPlainText("\n===remove()移除特定的字符");
    QString str1="你们，我们，他们";
    ui->plainTextEdit->appendPlainText("str1 = " + str1);

    QString DeStr="们";
    QChar DeChar=QChar(DeStr[0].unicode());   //获取汉字'的'的Unicode码，再创建QChar对象
    str1.remove(DeChar);        //str1="你，我，他"
    ui->plainTextEdit->appendPlainText("str1 = " + str1);

    //==== remove()
    ui->plainTextEdit->appendPlainText("\n===remove()移除右侧N个字符");
    str1="G:\\Qt6Book\\QtSamples\\qw.cpp";    //用到转移字符'\\'
    ui->plainTextEdit->appendPlainText(str1);

    int num = str1.lastIndexOf("\\");
    str1.remove(num + 1, 20);       //str1="G:\Qt6Book\QtSamples\"
    ui->plainTextEdit->appendPlainText(str1);
}

void Widget::on_pushButtonReplace_clicked()
{
    //==== replace(),替换指定的字符
    ui->plainTextEdit->appendPlainText("\n===replace(),替换指定的字符");
    QString str1 = "Goooogle";
    ui->plainTextEdit->appendPlainText(str1);

    str1.replace('o','e');
    ui->plainTextEdit->appendPlainText(str1);

    //==== replace()，替换字符串
    ui->plainTextEdit->appendPlainText("\n===replace(),替换字符串");
    str1 = "It is great";
    int num = str1.lastIndexOf(" ");
    ui->plainTextEdit->appendPlainText(str1);

    QString subStr = "wonderful";
    str1.replace(num + 1, subStr.size(), subStr);    //str1="It is wonderful"
    ui->plainTextEdit->appendPlainText(str1);

    str1.replace(num + 1, subStr.size(), "OK!");     //str1="It is OK!"
    qDebug(str1.toLocal8Bit().constData()); // qPrintable(str1) or qUtf8Printable(str1)
    ui->plainTextEdit->appendPlainText(str1);
}

void Widget::on_pushButtonClear_clicked()
{
    ui->plainTextEdit->clear();
}

